package com.azdebugit.ifelse.test;

import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Props;
import akka.actor.UntypedActor;
import akka.japi.Creator;
import lombok.Data;
import org.apache.commons.lang3.tuple.Pair;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Consumer;

/**
 * @Author: itdebug
 * @Email: itdebug
 * @version:: design_pattern_and_otherutils
 * @description:
 * @create: 2021/11/19 23:54.
 */
public class P8ActorTestIf {
    /**
     * Actor模型+领域驱动设计+策略模式+事件响应式架构
     * 真正的P8年薪百万架构师级写法，逼王才这么写代码，装逼的极限，内卷的奥义
     * Maven依赖：
     * 依赖Akka框架 Actor模型，懂得都懂，大数据分布式计算Spark框架RDD依赖的框架，很复杂，源码是Scala语言，逼王必学。
     * 也可以Scala做架构，Java做上层，有兴趣可以了解一下，反正管他是什么，够牛逼就完了。
     * 哎，就是得有牌面。if else什么的太low，应届毕业生水平才写if else（魔怔领导原话）。
     */
    private static String MAN = "man";
    private static String WOMAN = "woman";
    private static String WC_EVENT= "想上厕所";
    @Data
    static class Person extends UntypedActor {
        private String gender;
        private String name;

        public static Props props(final String name, final String gender) {
            return Props.create(new Creator<Person>() {
                private static final long serialVersionUID = 1L;
                @Override
                public Person create() throws Exception {
                    Person p = new Person();
                    p.setGender(gender);
                    p.setName(name);
                    return p;
                }
            });
        }
        @Override
        public void onReceive(Object message) throws Throwable {
            Pair<String, ActorRef> m = (Pair<String,ActorRef>)message;
            System.out.println(name + m.getLeft());
            m.getRight().tell(this, ActorRef.noSender());

        }
    }

    @Data
    static class Toilet extends UntypedActor {
        private static Map<String, Consumer<String>> FUNC_MAP = new ConcurrentHashMap<>();
        static {
            FUNC_MAP.put(MAN,person ->{System.out.println(person + "应该去男厕所");});
            FUNC_MAP.put(WOMAN,person ->{System.out.println(person + "应该去女厕所");});
        }

        public void wc(Person p ){
            FUNC_MAP.get(p.getGender()).accept(p.getName());
        }

        public static Props props() {
            return Props.create(Toilet.class);
        }

        @Override
        public void onReceive(Object message) throws Throwable {
            Person p = (Person) message;
            wc(p);
        }
    }

    public static void main(String[] args) {
        ActorSystem actorSystem = ActorSystem.create();
        ActorRef person = actorSystem.actorOf(Person.props("张三",MAN), "ZhangSan");
        ActorRef toilet = actorSystem.actorOf(Toilet.props(), "Toilet");
        Pair<String,ActorRef> message = Pair.of(WC_EVENT,toilet);
        person.tell(message,ActorRef.noSender());
    }
}
